package com.hhwy.frame.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.hhwy.frame.server.AppServer;
import com.hhwy.frame.server.IndexService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 创建人: wangpengfei
 * 创建日期: 2017/10/22
 * 功能描述:
 */
@RestController
public class LoginController {
    Logger log = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    AppServer appServer;
    @Autowired
    IndexService indexService;
    /**
     * 登陆
     * @param data
     * @param loginId
     * @param password
     * @return
     */
    @RequestMapping(value = "/appLogin", method = RequestMethod.POST)
    public String appLogin(@RequestBody String data, String  loginId, String password) {
        Map map = new HashMap();
        if((loginId=="admin"||"admin".equals(loginId)&&(password=="admin"||"admin".equals(password)))){
            map.put("code", "0");
            map.put("msg", "登陆成功");
        }else{
            map.put("code", "1");
            map.put("msg", "登陆失败,请重新登陆");
        }

        log.info(loginId+"在手机APP端登陆了");

        return JSON.toJSON(map).toString();
    }

    /**
     * 检测网络状态
     * @return
     */
    @RequestMapping(value = "/checkNetState", method = RequestMethod.GET)
    public String checkNetState() {
        Map map = new HashMap();
        map.put("code", "1");
        map.put("msg", "网络通信正常！");
        List<Map> list = indexService.getList();
        for (int i = 0; i < list.size(); i++) {
            log.info(list.get(i).toString());
        }
        map.put("name", JSONArray.toJSON(list).toString());
        return JSON.toJSON(map).toString();
    }


    /**
     * 获取工程列表
     * @return
     */
    @RequestMapping(value = "/getProjectList", method = RequestMethod.GET)
    public String getProjectList() {
        return JSONArray.toJSON(appServer.getProjectList()).toString();
    }

    @RequestMapping(value = "/getUserList", method = RequestMethod.GET)
    public String getUserList() {
        return JSONArray.toJSON(appServer.getUserList()).toString();
    }

    @RequestMapping(value = "/getMenuList", method = RequestMethod.GET)
    public String getMenuList() {
        return JSONArray.toJSON(appServer.getMenuList()).toString();
    }
}
